/*
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                   O\  =  /O
 *                ____/`- -'\____
 *              .'  \\|     |//  `.
 *             /  \\|||  :  |||//  \
 *            /  _||||| -:- |||||-  \
 *            |   | \\\  -  /// |   |
 *            | \_|  ''\- -/''  |   |
 *            \  .-\__  `-`  ___/-. /
 *          ___`. .'  /- . -\  `. . __
 *       ."" '<  `.___\_<|>_/___.'  >'"".
 *      | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *      \  \ `-.   \_ __\ /__ _/   .-` /  /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=- -='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *           佛祖保佑       永无BUG
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 */

package com.softeem.starwars.sound;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

/**
 * @Author mrchai 2020/4/11 23:54
 */
public class SoundPool extends Thread{

    public static final String BG_MUSIC = "background.mp3";
    public static final String SHOOT = "shoot.mp3";
    public static final String BOOM = "destroyed.mp3";

    private Player play;
    private String musicName;
    private boolean isLoop;

    public SoundPool(String musicName,boolean isLoop){
        this.musicName = musicName;
        this.isLoop = isLoop;
    }

    @Override
    public void run() {
        play();
    }

    public void play(){
        try {
            do{
                play = new Player(SoundPool.class.getResourceAsStream("/sound/"+musicName));
                play.play();
            }while(isLoop);
        } catch (JavaLayerException e) {
            e.printStackTrace();
        }
    }

    public void close(){
        isLoop = false;
        play.close();
        this.interrupt();
    }

}
